<?php

namespace app\admin\service;

use app\admin\model\AdEquip;
use app\admin\model\User;
use think\Db;

class AdEquipService extends BaseService
{
    /**
     * @var AdEquip
     */
    protected $model;

    public    $userId = 0;

    public function __construct()
    {
        $this->model = new AdEquip;
    }

    public function getList(): array
    {
        $param = request()->param();
        $where = [['mark', '=', 1]];
        if (!empty($param['organization'])) {
            $where[] = ['organization', '=', $param['organization']];
        }
        $schoolId = getSchooleId($this->userId);
        if ($schoolId) {
            $where[] = ['organization', '=', $schoolId];
        }
        if (!empty($param['scene_id'])) {
            $where[] = ['scene_id', '=', $param['scene_id']];
        }
        if (!empty($param['name'])) {
            $where[] = ['name', 'like', "%{$param['name']}%"];
        }
        $data = $this->model->order('id desc')
            ->with(['school', 'scene'])
            ->where($where)
            ->paginate(PERPAGE, false, ['page' => PAGE])
            ->each(
                function ($item) {
                    $item = $item->formatInfo($item);
                }
            );

        return [
            'msg'   => 'success',
            'code'  => 0,
            'data'  => $data->items(),
            'count' => $data->total()
        ];
    }

    public function edit()
    {
        $data = request()->param();
        $pwd  = get_password($data['password'] . $data['account']);
        if (empty($data['id'])) {
            if ((clone $this->model)->where('account', $data['account'])->value('id')) {
                return message('账号已存在，请重新添加', false);
            }
            if (User::where('username', $data['account'])->value('id')) {
                return message('账号已存在，请重新添加-', false);
            }
            $user             = [
                'school_id'   => $data['organization'],
                'user_type'   => 3,
                'realname'    => $data['name'],
                'nickname'    => $data['name'],
                'create_time' => time(),
                'username'    => $data['account'],
                'password'    => $pwd,
            ];
            $data['admin_id'] = (new User)->insertGetId($user);
        } else {
            $info = (clone $this->model)->find($data['id']);
            if (User::where('username', $data['account'])->where('id', '<>', $info['admin_id'])->value('id')) {
                return message('账号已存在，请重新添加-', false);
            }
            $update = [
                'realname' => $data['name'],
                'nickname' => $data['name'],
                'username' => $data['account'],
                'password' => $pwd,
            ];
            (new User)->where('id', $info['admin_id'])->update($update);
        }
        $data['resource'] = json_encode($data['resource']);
        if ($this->model->edit($data, $error)) {
            return message();
        }

        return message($error, false);
    }

    public function getResourceByAdminId($adminId): array
    {
        $info = $this->model->where('admin_id', $adminId)->find();
        if (empty($info)) {
            return [];
        }

        return [
            'resource' => json_decode($info->resource, true),
            'type'     => $info->resource_type
        ];
    }
}